package com.ems.dao;
import com.ems.model.Salary;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SalaryDAO {
    // 现有的 addSalary 方法
    public boolean addSalary(Salary salary) throws SQLException {
        String sql = "INSERT INTO salaries (emp_id, amount, pay_date) VALUES (?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, salary.getEmpId());
            stmt.setDouble(2, salary.getAmount());
            java.sql.Date sqlDate = new java.sql.Date(salary.getPayDate().getTime());
            stmt.setDate(3, sqlDate);
            return stmt.executeUpdate() > 0;
        }
    }

    // 添加 updateSalary 方法
    public boolean updateSalary(Salary salary) throws SQLException {
        String sql = "UPDATE salaries SET emp_id = ?, amount = ?, pay_date = ? WHERE salary_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, salary.getEmpId());
            stmt.setDouble(2, salary.getAmount());
            java.sql.Date sqlDate = new java.sql.Date(salary.getPayDate().getTime());
            stmt.setDate(3, sqlDate);
            stmt.setInt(4, salary.getSalaryId());
            return stmt.executeUpdate() > 0;
        }
    }

    // 添加 deleteSalary 方法
    public boolean deleteSalary(int salaryId) throws SQLException {
        String sql = "DELETE FROM salaries WHERE salary_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, salaryId);
            return stmt.executeUpdate() > 0;
        }
    }

    // 现有的 getAllSalaries 方法
    public List<Salary> getAllSalaries() throws SQLException {
        List<Salary> salaries = new ArrayList<>();
        String sql = "SELECT * FROM salaries";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql);
             ResultSet rs = stmt.executeQuery()) {
            while (rs.next()) {
                int salaryId = rs.getInt("salary_id");
                String empId = rs.getString("emp_id");
                double amount = rs.getDouble("amount");
                java.sql.Date payDate = rs.getDate("pay_date");

                Salary salary = new Salary(salaryId, empId, amount, payDate);
                salaries.add(salary);
            }
        }
        return salaries;
    }

    public Salary getSalaryById(int salaryId) throws SQLException {
        String sql = "SELECT * FROM salaries WHERE salary_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, salaryId);
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    return new Salary(
                            rs.getInt("salary_id"),
                            rs.getString("emp_id"),
                            rs.getDouble("amount"),
                            rs.getDate("pay_date")
                    );
                }
            }
        }
        return null;
    }
}